import time

from client.tdmg import manager
from dataTemp.user import User
from logo import Mlog
from process.Copy import Copy
from process.house import House
from process.machine import StateMachine
from process.state import Fight, PickStuff, FindWay, Through, Empty, leo_pre_room, EntryGame


class CopyBWJ(Copy):
    def __init__(self,user:User):
        super().__init__()
        self.user=user
        self.sm = StateMachine()
        self.sm.add_state("ft", Fight(self))
        self.sm.add_state("sf", PickStuff(self))
        self.sm.add_state("fd", FindWay(self))
        self.sm.add_state("ov", Through(self))
        self.sm.add_state("et", Empty(self))
        # self.sm.add_state("oc", OneCharacter(self))
        self.sm.add_state("szt_pre", leo_pre_room(self))
        # self.sm.add_state("choose", ChooseCharacter(self))
        self.sm.add_state("house", House(self))
        self.sm.add_state("eg", EntryGame(self))

        self.sm.set_initial_state("et")
        if self.is_at_town:
            self.sm.transition_to("house")
        # self.sm.set_initial_state("choose")
        self.sm.run_current_state()
        # td = threading.Thread(target=self.player.attack)
        # td.start()
        # self.leo_pre_room = True
        # td = threading.Thread(target=self.sm.run_current_state)
        # td.start()
        # self.run()
        manager.start_thread(target=self.get_prop, args=(manager.get_stop_event(),))
        manager.start_thread(target=self.run, args=(manager.get_stop_event(),))
        Mlog.logger.debug(f"当前对象 完成初始化")

    # 判断当前检测的敌人，与当前房间指定的敌人是否一致，如果不一致可能走出房间了
    def run(self, stop_event):
        while not stop_event.is_set():
            if self.is_at_town:
                # Mlog.logger.debug("在城镇中。。。")
                continue
            # Mlog.logger.debug(f"当前所有对象={self.props}")
            if self.props is None: continue
            self.set_prop()
            # Mlog.logger.error(f"当前所有对象2{self.props}")
            # self.check_room()
            # if self.player.position is None:
            #     # logo.Mlog.logger.error("一次角色也没有找到")
            #     self.player.random_move()
            #     continue
            self.sm.run_current_state()
            time.sleep(0.1)
            # self.run_action()
